package com.bilin.coin.activity;

import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bilin.coin.ApiConstant;
import com.bilin.coin.R;
import com.bilin.coin.bean.CommonSuccessEvent;
import com.bilin.coin.bean.InviteBean;
import com.bilin.coin.common.base.BaseAppCompatActivity;
import com.bilin.coin.network.RequestUtils;
import com.bilin.coin.tools.Geter;
import com.bilin.coin.utils.DensityUtils;
import com.bilin.coin.utils.QRCode;
import com.bilin.coin.utils.ShotShareUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = "/auction/InviteActivity")
public class InviteActivity extends BaseAppCompatActivity {


    @BindView(R.id.iv_code)
    ImageView iv_code;
    @BindView(R.id.tv_code)
    TextView tv_code;
    private InviteBean inviteBean;

    @Override
    protected void initView() {
        setTitle("推荐好友");
        setMenu(R.mipmap.share_icon);
        getData();
    }

    @Override
    public void setMenuOnClickListener() {
        new ShotShareUtil(this).shotShare2(this,iv_code);
    }

    @Override
    protected void initData() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_invite;
    }

    @OnClick({R.id.bt_confirm,R.id.tv_code_copy,R.id.bt_copy_link})
    public void onClick(View view) {

        switch (view.getId()) {
            case R.id.bt_confirm:
                //new ShotShareUtil(this).shotShare(this);
                try {
                    new ShotShareUtil(this).shotShare2(this,iv_code);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                break;
            case R.id.tv_code_copy:
                copeClipSuccess(tv_code);
                break;

            case R.id.bt_copy_link:
                copeClipSuccess(ApiConstant.DownLoadLink);
                break;

        }
    }

    private void getData() {
        new Geter(this,false,true) {
            @Override
            protected String fillUrl() {
                return "/api/user/getInviteMsg";
            }

            @Override
            protected Map<String, String> fillParams() {
                HashMap hashMap = new HashMap();
                return hashMap;
            }

            @Override
            protected void disposeSuccess(String dataJson) {
                super.disposeSuccess(dataJson);
                inviteBean = RequestUtils.getGson().fromJson(dataJson, InviteBean.class);
                tv_code.setText(inviteBean.getInviteCode());
                iv_code.setImageBitmap(QRCode.createQRCode(inviteBean.getLink(), DensityUtils.dp2px(InviteActivity.this, 200)));
            }
        };
    }

    /**
     * 登录成功
     *
     * @param commonSuccessEvent
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void loginSuccessEvent(CommonSuccessEvent commonSuccessEvent) {
        if (commonSuccessEvent.getType() == CommonSuccessEvent.LoginSuccess) {
            getData();
        }
        EventBus.getDefault().removeStickyEvent(commonSuccessEvent);
    }

}
